package com.cn.juc;

import java.util.concurrent.TimeUnit;

/***
 * @author: hels
 * @description:
 * @date: 2024-11-5 16:08
 **/
public class T03_Interrupte {
    public static void main(String[] args) throws InterruptedException {
        m2();
    }

    // interrupt() 设置标志位
    private static void m1() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            System.out.println("came to t1");
            while (true ) {
                if(Thread.currentThread().isInterrupted()) {
                    System.out.println("thread interrupte : " + Thread.currentThread().isInterrupted());
                    break;
                }
            }
            System.out.println("finish!!");
        });
        t1.start();
        TimeUnit.SECONDS.sleep(3);
        t1.interrupt();
    }

    /**
     * static interrupted() 检查是否有标志位，并且重置标志位
     */
    private static void m2() throws InterruptedException {
        Thread t2 = new Thread(() -> {
            System.out.println("came here!!");
            for (;;) {
                while (Thread.interrupted()) {
                    System.out.println("thread interrupt : " + Thread.currentThread().isInterrupted());
                    break;
                }
                break;
            }
        });
        t2.start();
        TimeUnit.SECONDS.sleep(3);
        System.out.println("active interrupt!!");
        t2.interrupt();

    }
    private static void m3() {

    }
    private static void m4() {

    }

}
